/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.prd.yueyuereader.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.prd.yueyuereader.R;
import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.util.AdError;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SplashActivity extends AppCompatActivity {

//    @Bind(R.id.tvSkip)
//    TextView tvSkip;
//
//    private boolean flag = false;
//    private Runnable runnable;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash);
//        ButterKnife.bind(this);
//
//        runnable = new Runnable() {
//            @Override
//            public void run() {
//                goHome();
//            }
//        };
//
//        tvSkip.postDelayed(runnable, 2000);
//
//        tvSkip.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                goHome();
//            }
//        });
//    }
//
//    private synchronized void goHome() {
//        if (!flag) {
//            flag = true;
//            startActivity(new Intent(SplashActivity.this, MainActivity.class));
//            finish();
//        }
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        flag = true;
//        tvSkip.removeCallbacks(runnable);
//        ButterKnife.unbind(this);
//    }

    private LinearLayout tengcp;//盛放腾讯广告的容器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //activity_main
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        initView();
    }

    //初始化控件的类
    private void initView() {
        tengcp=(LinearLayout) findViewById(R.id.tengcp1);
        getcp();//拉取广告-开屏广告的插入
        System.err.println("getcp()");
    }


    public  void getcp(){
        //参数 this, container, Constants.APPId, Constants.SplashPosId
        //创建开屏广告，广告拉取成功后会自动展示在container中。Container会首先被清空||测试1101152570  8863364436303842593
        //注意 Constants.APPId和Constants.SplashPosId后期要替换成自己的
        new SplashAD(SplashActivity.this, tengcp, "1106814424", "3020737202465416",
                new SplashADListener() {


                    //广告展示时间结束（5s）或者用户点击关闭时调用。
                    @Override
                    public void onADDismissed() {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(SplashActivity.this,
                                MainActivity.class);
                        //finish(); 改动-----
                        startActivity(intent);
                        finish();
                        System.out.println("onADDismissed");


                    }

                    @Override
                    public void onNoAD(AdError adError) {
                        System.out.println("adError"+adError);
                    }

                    //广告拉取成功开始展示时调用
                    @Override
                    public void onADPresent() {
                        // TODO Auto-generated method stub

                        System.out.println("广告拉取成功");
                    }

                    @Override
                    public void onADClicked() {

                    }

                    @Override
                    public void onADTick(long l) {

                    }

                    //广告拉取超时（3s）或者没有广告时调用，errCode参见SplashAd类的常量声明
                    //@Override
                    public void onNoAD(int arg0) {
                        System.out.println("arg0"+arg0);
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(SplashActivity.this,
                                MainActivity.class);
                        finish();
                        startActivity(intent);


                    }



                });


    }

}
